package com.wxy.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wxy.dao.UserDao;
import com.wxy.dao.UserImgDao;
import com.wxy.domain.MyException;
import com.wxy.domain.R;
import com.wxy.domain.User;
import com.wxy.domain.UserImg;
import com.wxy.service.UpdateUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author 47868
 */
@Service("UpdateUserService")
public class UpdateUserServiceImpl implements UpdateUserService {
    private final UserDao userDao;
    private final StringRedisTemplate stringRedisTemplate;
    private final UserImgDao userImgDao;
    private static final String PREFIX_PATH ="D:\\代码\\LibSystem\\userImg\\";
    private static final String SUFFIX_PATH =".jpg";
    @Autowired
    public UpdateUserServiceImpl(UserDao userDao,
                                 StringRedisTemplate stringRedisTemplate,
                                 UserImgDao userImgDao) {
        this.userDao = userDao;
        this.stringRedisTemplate = stringRedisTemplate;
        this.userImgDao = userImgDao;
    }

    @Override
    public R updateUsername(String newUsername,int id) {
        User user=userDao.selectOne(new QueryWrapper<User>().eq("username", newUsername));
        if(user!=null){
            return R.error(403,"用户名已存在...");
        }
        String oldUsername=userDao.selectOne(new QueryWrapper<User>().eq("id",id)).getUsername();
        userDao.updateById(newUsername,id);
        stringRedisTemplate.delete("user:"+oldUsername);
        return R.ok("信息修改成功");
    }

    @Override
    public R updateEmail(String newEmail,int id) {
        User user=userDao.selectOne(new QueryWrapper<User>().eq("id",id));
        String oldUsername=user.getUsername();
        userDao.updateEmail(newEmail,id);
        stringRedisTemplate.delete("user:"+oldUsername);
        return R.ok("更新成功");
    }
    @Override
    public R verifyPassword(String oldPassword, int id) {
        String password=userDao.selectOne(new QueryWrapper<User>().eq("id",id)).getPassword();
        if(oldPassword.equals(password)){
            return R.ok("验证成功");
        }
        return R.error(403,"验证失败");
    }

    @Override
    public R changePassword(String newPassword, int id) {
        String oldUsername=userDao.selectOne(new QueryWrapper<User>().eq("id",id)).getUsername();
        userDao.updatePasswordById(newPassword,id);
        stringRedisTemplate.delete("user:"+oldUsername);
        return R.ok("修改成功");
    }

    public static void println(Object message) {
        System.out.println(message);
    }

    @Override
    public R updateAvatar(MultipartFile file, int id) {
        User user=userDao.selectOne(new QueryWrapper<User>().eq("id",id));
        if(file.isEmpty()){
            return R.error(403,"失败");
        }
        String type=file.getContentType();
        String suffix="image/jpeg";
        if(!suffix.equals(type)){
            return R.error(403,"失败");
        }
        String path=PREFIX_PATH+user.getId()+SUFFIX_PATH;
        try {
            file.transferTo(new File(path));
        }
        catch (Exception e) {
            println(e.getMessage());
            throw new MyException("图片上传异常...");
        }
        String dbPath=user.getId()+SUFFIX_PATH;
        userImgDao.update(new UpdateWrapper<UserImg>().eq("uid",user.getCardId()).set("path",dbPath));
        return R.ok("更新成功");
    }
}
